﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using Golos.Polls.PollCache;
using Golos.Web.Utils;
using iPoint.Server.Configuration;

namespace Golos.Web.handlers
{
    public class VoteHandler : HandlerBase
    {
        public VoteHandler(HttpContext httpContext)
            : base(httpContext)
        {
        }
        public override void Handle()
        {
            try
            {
                Token token = TokenUtils.FromCookies(_httpContext.Request.Cookies);

                if (token != null)
                {
                    

                    if (GetQueryParam("qid") != null && GetQueryParam("aid") != null)
                    {
                        try
                        {
                            Guid qid = new Guid(GetQueryParam("qid"));
                            Guid aid = new Guid(GetQueryParam("aid"));
                            string ip = _httpContext.Request.UserHostAddress;

                            if (PollsClient.GetUserAnswerInfo(qid, token.User) != null)
                            {
                                LogClass.WriteWarningEntry("User: " + token.User + " уже голосовал в опросе " + qid);
                                _httpContext.Response.Write("{ \"warn\": \"Вы уже голосовали в данном опросе.\" }");
                            }
                            else
                            {
                                LogClass.WriteInfoEntry("User: " + token.User + " голосует qid=" + qid + ", aid=" + aid);

                                UserAnswerInfo userAnswerInfo = new UserAnswerInfo(token.User, qid, aid);
                                AnswerProperties answerProperties = new AnswerProperties(aid, ip, true);

                                LogClass.WriteDebugEntry("User: " + token.User + " start vote.");

                                if (PollsClient.Vote(userAnswerInfo, answerProperties))
                                {
                                    string json = "{" + PollsHelper.GetVotedPollInfo(token.User, qid).TrimEnd(new char[] { ',', ' ' }) + "}";
                                    _httpContext.Response.Write(json);
                                }
                                else
                                {
                                    _httpContext.Response.Write("{ \"error\": \"Вы не можете голосовать.\" }");
                                }
                                LogClass.WriteDebugEntry("User: " + token.User + " end vote.");
                            }
                        }
                        catch (Exception e)
                        {
                            LogClass.WriteErrorEntry(e.ToString());
                            _httpContext.Response.Write("{ \"error\": \"Не удалось проголосовать.\" }");
                        }
                    }
                    else
                    {
                        LogClass.WriteDebugEntry("User: " + token.User + "qid & aid пустые");
                        _httpContext.Response.Write("{ \"error\": \"Переданы некорректные параметры\" }");
                    }
                }
                else
                {
                    LogClass.WriteInfoEntry("Попытка голосования без куки.");
                    _httpContext.Response.Write("{ \"error\": \"Вы не авторизовались.\" }");
                }
            }
            catch (Exception e)
            {
                LogClass.WriteErrorEntry(e.ToString());
            }
        }
    }
}